/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralelements;

import org.openaecfm.model.structuralelements.ReinforcingElement;
import org.openaecfm.model.structuralelements.Tendon;
import org.openaecfm.model.structuralelements.TendonTypeEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class TendonIO extends AbstractEntityInstanceInputImpl<Tendon>
		implements IEntityInstanceOutput<Tendon> {
	private IEntityInstanceUtilInput<ReinforcingElement> utilInput;
	private IEntityInstanceUtilOutput<ReinforcingElement> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<ReinforcingElement> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Tendon pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTENDON",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set PredefinedType
		ei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));

		// Set NominalDiameter
		ei.addParameter(new RealParameter(pojo.getNominalDiameter()));

		// Set CrossSectionArea
		ei.addParameter(new RealParameter(pojo.getCrossSectionArea()));

		// Set TensionForce
		if (pojo.getTensionForce() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTensionForce()));
		}

		// Set PreStress
		if (pojo.getPreStress() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getPreStress()));
		}

		// Set FrictionCoefficient
		if (pojo.getFrictionCoefficient() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getFrictionCoefficient()));
		}

		// Set AnchorageSlip
		if (pojo.getAnchorageSlip() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getAnchorageSlip()));
		}

		// Set MinCurvatureRadius
		if (pojo.getMinCurvatureRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getMinCurvatureRadius()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Tendon doCreateObject() {
		return new Tendon();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Tendon pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<ReinforcingElement> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, Tendon pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		EnumerationParameter pretype = (EnumerationParameter) sei
				.getParameter(9);
		pojo.setPredefinedType(TendonTypeEnum.valueOf(pretype.toEnumString()));

		// Set NominalDiameter
		RealParameter nodiameter = (RealParameter) sei.getParameter(10);
		pojo.setNominalDiameter(nodiameter.toDouble());

		// Set CrossSectionArea
		RealParameter crossarea = (RealParameter) sei.getParameter(11);
		pojo.setCrossSectionArea(crossarea.toDouble());

		// Set TensionForce
		Parameter tensionforce = sei.getParameter(12);
		if (tensionforce.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) tensionforce;
			pojo.setTensionForce(r.toDouble());
		}

		// Set PreStress
		Parameter prestress = sei.getParameter(13);
		if (prestress.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) prestress;
			pojo.setPreStress(r.toDouble());
		}

		// Set FrictionCoefficient
		Parameter fric = sei.getParameter(14);
		if (fric.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) fric;
			pojo.setFrictionCoefficient(r.toDouble());
		}

		// Set AnchorageSlip
		Parameter anchslip = sei.getParameter(15);
		if (anchslip.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) anchslip;
			pojo.setAnchorageSlip(r.toDouble());
		}

		// Set MinCurvatureRadius
		Parameter min = sei.getParameter(16);
		if (min.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) min;
			pojo.setMinCurvatureRadius(r.toDouble());
		}
	}
}
